package com.ruiyun.ruinews.thread;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by zouhui on 16/1/11.
 */
public class ImageThread extends Thread {
    private String url = new String();
    private Handler handler;

    public ImageThread(Handler handler, String url) {
        this.url = url;
        this.handler = handler;
    }

    @Override
    public void run() {
        super.run();
        try {
            URL u = new URL(url);

            HttpURLConnection connection = (HttpURLConnection) u.openConnection();
            connection.connect();
            InputStream is = connection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            Message msg = new Message();
            msg.what = 200;
            msg.obj = bitmap;
            handler.sendMessage(msg);
            Log.e("********", "is:132654564564231546");
            is.close();
            connection.disconnect();
            handler.sendEmptyMessage(100);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
